import 'dart:io';

import 'package:common_utils/common_utils.dart';
import 'package:easy_refresh/easy_refresh.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:x_core/x_core.dart';
import 'package:x_module_copy/res/string_res/str_res_en.dart';
import 'package:x_module_copy/res/string_res/str_res_zh.dart';
import 'package:x_module_copy/router/copy_router.dart';
import 'package:x_project/res/string_res/str_res_keys.dart';
import 'package:x_project/x_project.dart';

void main() {
  // 强制竖屏
  WidgetsFlutterBinding.ensureInitialized(); //必选先初始化，不然报错
  SystemChrome.setPreferredOrientations([
    DeviceOrientation.portraitUp,
    DeviceOrientation.portraitDown,
  ]);
  // 透明状态栏
  if (Platform.isAndroid) {
    SystemUiOverlayStyle sos =
        const SystemUiOverlayStyle(statusBarColor: Colors.transparent);
    SystemChrome.setSystemUIOverlayStyle(sos);
  }

  runApp(const MainApp());
}

class MainApp extends StatelessWidget {
  const MainApp() : super(key: const Key("MainApp"));

  void init(BuildContext context) {
    //初始化本地持久化
    SPUtils.init();
    //初始化网络dio
    DioHolder.init(ApiConstant.baseUrl);
    //初始化路由
    Get.addPages(CommonRouter.routes);
    //初始化日志
    LogUtil.init(isDebug: !XConstant.inProduction, tag: "Flutter GetX Project");
    //初始化各模块国际化
    enUSRes.addAll(enUSResMain);
    zhCNRes.addAll(zhCNResMain);

    //全局控制器
    Get.put(GlobalController());

    //配置EasyRefresh默认刷新头尾
    EasyRefresh.defaultHeaderBuilder = () {
      return MaterialHeader(
        valueColor: AlwaysStoppedAnimation(Get.theme.primaryColor),
      );
    };
    EasyRefresh.defaultFooterBuilder = () {
      return ClassicFooter(
        textStyle: const TextStyle(color: XColors.textColor99),
        backgroundColor: Colors.transparent,
        dragText: SR.dragText.tr,
        armedText: SR.armedText.tr,
        readyText: SR.readyText.tr,
        processingText: SR.processingText.tr,
        processedText: SR.processedText.tr,
        noMoreText: SR.noMoreText.tr,
        failedText: SR.failedText.tr,
        messageText: SR.messageText.tr,
        showMessage: false,
      );
    };
    EasyLoading.instance
      ..loadingStyle = EasyLoadingStyle.custom
      ..indicatorType = EasyLoadingIndicatorType.wave
      ..backgroundColor = XColors.textColor33
      ..indicatorColor = Colors.white
      ..textColor = Colors.white
      ..progressColor = Colors.white
      ..maskType = EasyLoadingMaskType.none
      ..toastPosition = EasyLoadingToastPosition.center
      ..animationStyle = EasyLoadingAnimationStyle.opacity
      ..textAlign = TextAlign.center
      ..indicatorSize = 40.w
      ..radius = 12.w
      ..fontSize = 18.sp
      ..progressWidth = 2.w
      ..lineWidth = 4.w
      ..displayDuration = const Duration(milliseconds: 2000)
      ..animationDuration = const Duration(milliseconds: 200)
      ..textPadding = EdgeInsets.only(bottom: 10.w)
      ..contentPadding = EdgeInsets.symmetric(
        vertical: 15.w,
        horizontal: 22.w,
      );
  }

  @override
  Widget build(BuildContext context) {
    return ScreenUtilInit(
      designSize: const Size(750, 1624),
      builder: (BuildContext context, Widget? child) => GetMaterialApp(
        title: 'Copy Module',
        debugShowCheckedModeBanner: false,
        enableLog: true,
        initialRoute: CopyRouter.copyPage,
        getPages: CopyRouter.routes,
        unknownRoute: CommonRouter.unknownRoute,
        translations: TranslationService(),
        locale: TranslationService.locale,
        fallbackLocale: TranslationService.fallbackLocale,
        theme: themeDataLight,
        builder: EasyLoading.init(builder: (context, widget) {
          init(context);
          TranslationService.initNetString();
          return widget!;
        }),
      ),
    );
  }
}
